# python有两种错误很容易辨认：语法错误(解析错)和异常
import sys
import utils.Logger as logger
from utils import Logger


# 如果直接执行会抛出异常
# file = open("D:/not_exist ")


# 定义自己的异常
class MyError(Exception):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)


# 故意抛出ZeroDivisionError: division by zero
def method1():
    return 1/0


# 故意抛出IOError
def method2():
    not_ext_file = open("D:/not_exist ")
    not_ext_file.close()


try:
    # method1()
    method2()
except IOError as ioErr:
    logger.error("IOError occurred!" + str(ioErr))
    # 抛出其他异常
    raise MyError("MmyException thrown")
except:
    logger.error("unknown exception occurred!" + str(sys.exc_info()[0]))
    # 抛出当前异常本身
    raise
else:
    Logger.info("no exception")
finally:
    Logger.info("finally executed")



